﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using Zsnd.Helpers;
using MaymuseMS.DAO;
using MaymuseMS.DAL;
using MaymuseMS.Models;
using MaymuseMS.Models.ValueObject;
using LinqToExcel;

namespace MaymuseMS.Controllers {
    public class ExcelController : Controller {
        UnitOfWork unitOfWork = new UnitOfWork();
        public ActionResult Index() {
            //Parts();
            //Semifinished();
            //Finished();
            //Supplier();
            //unitOfWork.Save();
            return View();
        }

        public void Finished() {
            var excel = new ExcelQueryFactory(@"C:\Users\ZsnD\Desktop\成品物料编码.xls");
            var a = excel.WorksheetNoHeader(0);
            var b = a.Where(p => p[0] != "")
                .Select(p => new {
                    Number = p[0], Name = p[1], Model = p[2], Remark = p[4],
                    Count = p[3]
                });


            foreach (var c in b) {
                Finished Finished = new Finished {
                    Name = c.Name, Number = c.Number,
                    Model = c.Model
                };
                string number = c.Number;
                if (!string.IsNullOrWhiteSpace(number)) {

                    unitOfWork.FinishedRepository.Add(Finished);
                    //下面代码要求，手动入库的话，如果库存还未建立，必然需要在基础零件表中已经存在相关
                    //物品编号的零件
                    ProductValueObject productValueObject = new ProductValueObject {
                        Model = Finished.Model,
                        Name = Finished.Name,
                        Number = Finished.Number,
                        Spec = Finished.Spec,
                        Unit = Finished.Unit
                    };
                    FinishedInDepository FinishedInDepository = new FinishedInDepository {
                        Date = DateTime.Now,
                        ProductValueObject = productValueObject,
                        Count = double.Parse(c.Count),
                        Number = "11120901"
                    };

                    //找库存
                    var FinishedDepository = unitOfWork.FinishedDepositoryRepository.Get(number);
                    //库存不存在
                    if (FinishedDepository == null) {
                        FinishedDepository = new FinishedDepository {
                            ProductValueObject = productValueObject, Remark = c.Remark,
                            DepositoryItems = new List<DepositoryItem>()
                        };
                        unitOfWork.FinishedDepositoryRepository.Add(FinishedDepository);
                    }
                    //库存项目存在，直接加数量。不存在，库存加一个项
                    var depositoryItem = FinishedDepository.DepositoryItems.Where(d => d.Number == FinishedInDepository.Number).FirstOrDefault();
                    if (depositoryItem != null) {
                        depositoryItem.Count += FinishedInDepository.Count;
                    } else {
                        depositoryItem = new DepositoryItem {
                            Number = FinishedInDepository.Number, Count = FinishedInDepository.Count, Location = FinishedInDepository.Location
                        };
                        FinishedDepository.DepositoryItems.Add(depositoryItem);
                    }

                    //插入库记录
                    unitOfWork.FinishedInDepositoryRepository.Add(FinishedInDepository);
                }
            }
        }
        public void Semifinished() {
            var excel = new ExcelQueryFactory(@"C:\Users\ZsnD\Desktop\Copy.xls");
            var a = excel.WorksheetNoHeader(1);
            var b = a.Where(p => p[0] != "")
                .Select(p => new {
                    Number = p[0], Name = p[1], Spec = p[2], Unit = p[4],
                    Count = p[3]
                });


            foreach (var c in b) {
                Semifinished Semifinished = new Semifinished {
                    Name = c.Name, Number = c.Number,
                    Spec = c.Spec, Unit = c.Unit
                };
                string number = c.Number;
                if (!string.IsNullOrWhiteSpace(number)) {

                    unitOfWork.SemifinishedRepository.Add(Semifinished);
                    //下面代码要求，手动入库的话，如果库存还未建立，必然需要在基础零件表中已经存在相关
                    //物品编号的零件
                    ProductValueObject productValueObject = new ProductValueObject {
                        Model = Semifinished.Model,
                        Name = Semifinished.Name,
                        Number = Semifinished.Number,
                        Spec = Semifinished.Spec,
                        Unit = Semifinished.Unit
                    };
                    SemifinishedInDepository SemifinishedInDepository = new SemifinishedInDepository {
                        Date = DateTime.Now,
                        ProductValueObject = productValueObject,
                        Count = double.Parse(c.Count),
                        Number = "11120901"
                    };

                    //找库存
                    var SemifinishedDepository = unitOfWork.SemifinishedDepositoryRepository.Get(number);
                    //库存不存在
                    if (SemifinishedDepository == null) {
                        SemifinishedDepository = new SemifinishedDepository {
                            ProductValueObject = productValueObject, Remark = "",
                            DepositoryItems = new List<DepositoryItem>()
                        };
                        unitOfWork.SemifinishedDepositoryRepository.Add(SemifinishedDepository);
                    }
                    //库存项目存在，直接加数量。不存在，库存加一个项
                    var depositoryItem = SemifinishedDepository.DepositoryItems.Where(d => d.Number == SemifinishedInDepository.Number).FirstOrDefault();
                    if (depositoryItem != null) {
                        depositoryItem.Count += SemifinishedInDepository.Count;
                    } else {
                        depositoryItem = new DepositoryItem {
                            Number = SemifinishedInDepository.Number, Count = SemifinishedInDepository.Count, Location = SemifinishedInDepository.Location
                        };
                        SemifinishedDepository.DepositoryItems.Add(depositoryItem);
                    }

                    //插入库记录
                    unitOfWork.SemifinishedInDepositoryRepository.Add(SemifinishedInDepository);
                }
            }
        }
        public void Parts() {
            var excel = new ExcelQueryFactory(@"C:\Users\ZsnD\Desktop\Copy.xls");
            var a = excel.WorksheetNoHeader(0);
            var b = a.Where(p => p[0] != "")
                .Select(p => new {
                    Number = p[0], Name = p[1], Spec = p[2], Unit = p[4],
                    Count = p[3]
                });


            foreach (var c in b) {
                Parts Parts = new Parts {
                    Name = c.Name, Number = c.Number,
                    Spec = c.Spec, Unit = c.Unit
                };
                string number = c.Number;
                if (!string.IsNullOrWhiteSpace(number)) {

                    unitOfWork.PartsRepository.Add(Parts);
                    //下面代码要求，手动入库的话，如果库存还未建立，必然需要在基础零件表中已经存在相关
                    //物品编号的零件
                    ProductValueObject productValueObject = new ProductValueObject {
                        Model = Parts.Model,
                        Name = Parts.Name,
                        Number = Parts.Number,
                        Spec = Parts.Spec,
                        Unit = Parts.Unit
                    };
                    PartsInDepository PartsInDepository = new PartsInDepository {
                        Date = DateTime.Now,
                        ProductValueObject = productValueObject,
                        Count = double.Parse(c.Count),
                        Number = "11120901"
                    };

                    //找库存
                    var PartsDepository = unitOfWork.PartsDepositoryRepository.Get(number);
                    //库存不存在
                    if (PartsDepository == null) {
                        PartsDepository = new PartsDepository {
                            ProductValueObject = productValueObject, Remark = "",
                            DepositoryItems = new List<DepositoryItem>()
                        };
                        unitOfWork.PartsDepositoryRepository.Add(PartsDepository);
                    }
                    //库存项目存在，直接加数量。不存在，库存加一个项
                    var depositoryItem = PartsDepository.DepositoryItems.Where(d => d.Number == PartsInDepository.Number).FirstOrDefault();
                    if (depositoryItem != null) {
                        depositoryItem.Count += PartsInDepository.Count;
                    } else {
                        depositoryItem = new DepositoryItem {
                            Number = PartsInDepository.Number, Count = PartsInDepository.Count, Location = PartsInDepository.Location
                        };
                        PartsDepository.DepositoryItems.Add(depositoryItem);
                    }

                    //插入库记录
                    unitOfWork.PartsInDepositoryRepository.Add(PartsInDepository);
                }
            }
        }

        public void Supplier() {
            var excel = new ExcelQueryFactory(@"C:\Users\ZsnD\Desktop\供应商资料.xls");
            var a = excel.WorksheetNoHeader(0);
            var b = a.Where(p => p[0] != "")
                .Select(p => new {
                    Number = p[0], Name = p[1], Address = p[2], bank = p[3], banknu = p[4], tel = p[5], fax = p[6],
                    url = p[7], su1 = new SupplierUser {
                        Name = p[8], Department = p[9], Post = p[10], Phone = p[11], Telephone = p[12],
                        Email = p[13]
                    }, su2 = new SupplierUser {
                        Name = p[14], Department = p[15], Post = p[16], Phone = p[17], Telephone = p[18],
                        Email = p[19]
                    }
                });
            foreach (var c in b) {
                Supplier supplier = new Supplier();
                supplier.Number = c.Number;
                supplier.Name = c.Name;
                supplier.Contacts = new Contacts();
                supplier.Contacts.Address = c.Address;
                supplier.Banks = new Bank();
                supplier.Banks.Name = c.bank;
                supplier.Banks.Account = c.banknu;
                supplier.Contacts.Phone = c.tel;
                supplier.Contacts.Fax = c.fax;
                supplier.WebSiteUrl = c.url;
                SupplierUser user1 = c.su1;
                SupplierUser user2 = c.su2;
                IList<SupplierUser> users = new List<SupplierUser>();
                if (!string.IsNullOrWhiteSpace(user1.Name)) users.Add(user1);
                if (!string.IsNullOrWhiteSpace(user2.Name)) users.Add(user2);
                if (users.Count > 0)
                    supplier.SupplierUsers = users;
                unitOfWork.SupplierRepository.Add(supplier);

            }
            
        }
    }
}
